import hashlib
import time

import requests

# 创建一个全局会话对象
sess = requests.session()

# 配置接口信息
IP = "localhost"
PORT = "8080"
BASE_URL = "http://" + IP + ":" + PORT


# 通用接口练习
class CommonApi:
    # 1.request基础 获取网页信息
    @staticmethod
    def fun():
        # 发起http请求  : method请求方式 URL 资源地址
        url = BASE_URL + "/pinter"
        rp = sess.request(method="get", url=url)
        # 获取 响应体的原始数据
        print(rp.text)
        print(rp.headers)

    # 普通get类型请求接口
    @staticmethod
    def fun1():
        api_url = "/pinter/com/getSku"
        url = BASE_URL + api_url
        params = {"id": 1}
        rp = sess.request(method="get", url=url, params=params)
        # 获取 响应体原始数据 text json
        print(rp.text)
        rp1 = rp.json()
        print(rp1["message"])

    #  参数为 K=V的POST接口
    @staticmethod
    def fun2(userName,password,api_url = "/pinter/com/login" ):
        api_url = api_url
        url = BASE_URL + api_url
        data = {"userName": userName, "password": password}
        rp = sess.request(method="post", url=url, data=data)
        print(rp.text)
        rp1 = rp.json()
        print(rp1["message"])
        return rp1["message"]

    # 参数为json的POST接口
    @staticmethod
    def test_register_success(**kwargs):
        # 接口地址
        api_url = "/pinter/com/register"
        url = BASE_URL + api_url
        headers = {"Content-Type":"application/json"}
        data = {
                "userName": kwargs.get("userName"),
                "password": kwargs.get("password"),
                "gender": kwargs.get("gender",1),  # 1男 2女
                "phoneNum": kwargs.get("phone",""),
                "email": kwargs.get("email","")
                }
        # 发送请求 response,参数为json格式
        response = requests.post(url,json=data,headers=headers)
        result = response.json()
        # print(result)
        return result["message"]
        # rp = sess.request(method="post", url=url, json=data)
        # print(rp.text)
        # rp1 = rp.json()
        # print(rp1["message"])

    # 参数为k=json的POST接口
    @staticmethod
    def fun4():
        api_url = "/pinter/com/buy"
        url = BASE_URL + api_url
        data = {'params': {"skuId": 123, "num": 10}}
        rp = sess.request(method="post", url=url, data=data)
        print(rp.text)
        rp1 = rp.json()
        print(rp1["message"])

    # 需要签名验证的POST接口
    @staticmethod
    def fun5():
        pnum = "18291070627"
        opt = "testfan"
        times = int(time.time() * 1000)
        t = pnum + opt + str(times)
        sign = hashlib.md5(t.encode())

        api = "/pinter/com/userInfo"
        url = BASE_URL + api
        json = {"phoneNum": pnum, "optCode": opt, "timestamp": times, "sign": sign.hexdigest()}
        rp = sess.request(method="post", url=url, json=json)
        print(rp.text)
        rp1 = rp.json()
        print(rp1)

    # 返回json数组的GET接口
    @staticmethod
    def fun6():
        api_url = "/pinter/com/getSku"
        url = BASE_URL + api_url
        params = {"id": 1}
        rp = sess.request(method="get", url=url, params=params)
        # 获取 响应体原始数据 text json
        print(rp.text)
        rp1 = rp.json()
        print(rp1["message"])


class RestfulApi:
    @staticmethod
    def get_api(apiurl="/pinter/com/phone/", id=1):
        api_url = apiurl + str(id)
        url = BASE_URL + api_url
        print(url)
        rp = sess.request(method="get", url=url)
        print(rp.text)
        rp1 = rp.json()
        print(rp1["message"])

    @staticmethod
    def post_api(apiurl="/pinter/com/phone/"):
        api_url = apiurl
        url = BASE_URL + api_url
        # 接收 请求参数
        json = {"brand": {"brand": "Huawei", "color": "yellow", "memorySize": "64G", "cpuCore": "8核", "price": "8848",
                          "desc": "全新上市"}}
        rp = sess.request(method="get", url=url, json=json)
        print(rp.text)
        rp1 = rp.json()
        print(rp1["message"])


class CookieAndToken:
    @staticmethod
    def cookie_api():
        # 1. 先登录
        login_api_url = "/pinter/bank/api/login"
        url = BASE_URL + login_api_url
        # json 接收get请求参数
        data = {"userName":"admin","password":"123456"}
        # print(url)
        rp = sess.request(method="post", url=url, data=data)
        # 获取 响应体的原始数据  text    , json
        print(rp.text)


        # 2. 查询余额
        query_api_url = "/pinter/bank/api/query"
        url = BASE_URL + query_api_url

        params={"userName":"admin"}

        rp = sess.request(method="get",url=url,params=params)
        print(rp.text)

    @staticmethod
    def token_api():
        # 1. 先登录
        login_api_url = "/pinter/bank/api/login2"
        url = BASE_URL + login_api_url
        # json 接收get请求参数
        data = {"userName":"admin","password":"123456"}
        # print(url)
        rp = sess.request(method="post", url=url, data=data)
        # 获取 响应体的原始数据  text    , json
        print(rp.text)
        rp = rp.json()
        testfan_token = rp["data"]


        # 2. 查询余额
        query_api_url = "/pinter/bank/api/query2"
        url = BASE_URL + query_api_url

        params={"userName":"admin"}
        # 请求头配置 headers
        headers = {"testfan-token": testfan_token}
        rp = sess.request(method="get",url=url,params=params ,headers=headers)
        print(rp.text)


class File:
    @staticmethod
    def file_up():
        enter_api = "/pinter/file/api/upload2"
        URL = BASE_URL + enter_api
        # 打开文件的路径
        path = "./cp1.jpg"
        with open(path, "rb") as f:
            files = {"file": (path, f, "image/*")}
            rp = sess.request(method="post", url=URL, files=files)
            print(rp.text)

    @staticmethod
    def file_download():
        apiurl = "/pinter/file/api/download"
        url = BASE_URL + apiurl
        params = {"id": 3}
        rp = sess.request(method="get", url=url, params=params)
        print(rp.content)




if __name__ == '__main__':
    # CommonApi.fun2("admin",123456)
    CommonApi.test_register_success()

    # RestfulApi.post_api()
    # CookieAndToken.cookie_api()
    # CookieAndToken.token_api()
    # File.file_up()
    # File.file_download()


